package com.cvmars.jianyou.module.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cvmars.jianyou.R;
import com.cvmars.jianyou.api.api.Api;
import com.cvmars.jianyou.api.api.HttpUtils;
import com.cvmars.jianyou.api.api.SimpleSubscriber;
import com.cvmars.jianyou.api.model.HttpResult;
import com.cvmars.jianyou.config.MyConfig;
import com.cvmars.jianyou.model.ReadStatus;
import com.cvmars.jianyou.model.UserInfoManager;
import com.cvmars.jianyou.model.UserModel;
import com.cvmars.jianyou.module.activity.AdviceActivity;
import com.cvmars.jianyou.module.activity.FollowActivity;
import com.cvmars.jianyou.module.activity.LoginActivity;
import com.cvmars.jianyou.module.activity.MeAccountActivity;
import com.cvmars.jianyou.module.activity.MeInfoActivity;
import com.cvmars.jianyou.module.activity.PersonHomeActivity;
import com.cvmars.jianyou.module.activity.SettingActivity;
import com.cvmars.jianyou.module.activity.VipActivity;
import com.cvmars.jianyou.module.activity.WhoSeeMeActivity;
import com.cvmars.jianyou.module.activity.XiangQingActivity;
import com.cvmars.jianyou.module.activity.XiangQingDaTingActivity;
import com.cvmars.jianyou.module.base.BaseFragment;
import com.cvmars.jianyou.module.model.AppConfigModel;
import com.cvmars.jianyou.ui.CircleImageView;
import com.cvmars.jianyou.ui.ShareActivity;
import com.cvmars.jianyou.utils.ClickUtils;
import com.cvmars.jianyou.utils.ShareUtils;
import com.orhanobut.hawk.Hawk;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by hehaifeng on 2018/8/9.
 */

public class MeSettingFragment extends BaseFragment implements View.OnClickListener {


    LinearLayout llLogin;
    @BindView(R.id.rl_zican)
    RelativeLayout rlZican;
    @BindView(R.id.rl_advice)
    RelativeLayout rlAdvice;
    @BindView(R.id.rl_setting_info)
    RelativeLayout rlSettingInfo;
    @BindView(R.id.rl_home)
    RelativeLayout rlHome;
    @BindView(R.id.iv_avator)
    CircleImageView ivAvator;
    @BindView(R.id.txt_user_name)
    TextView txtUserName;
    @BindView(R.id.txt_user_sign)
    TextView txtUserSign;
    @BindView(R.id.txt_guanzhu)
    TextView txtGuanzhu;
    @BindView(R.id.txt_fans)
    TextView txtFans;
    @BindView(R.id.txt_scan_number)
    TextView txt_scan_number;
    @BindView(R.id.rl_xiangqing)
    RelativeLayout rlXiangqing;

    @BindView(R.id.rl_vip)
    RelativeLayout rlVip;

    private Unbinder unbinder;
    UserModel userModel;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        View view = inflater.inflate(R.layout.frag_home_setting, null);
        unbinder = ButterKnife.bind(this, view);
        llLogin = view.findViewById(R.id.ll_login);
//        llLogin.setOnClickListener(this);


        UserModel model = Hawk.get(MyConfig.SP_CACHE_USERMODEL);

        if (model != null) {
            userModel = model;
            UserInfoManager.uid = model.id;
            txtUserName.setText(model.name);
            txtUserSign.setText(model.intro);

            Glide.with(getActivity()).load(model.avatar_url).into(ivAvator);

        }

        return view;
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.ll_login:


                getActivity().startActivity(new Intent(getActivity(), LoginActivity.class));
                break;


        }
    }

    /**
     * onDestroyView中进行解绑操作
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.rl_zican, R.id.rl_advice, R.id.rl_setting, R.id.rl_setting_info, R.id.rl_home, R.id.rl_vip, R.id.txt_guanzhu, R.id.txt_fans})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_zican:
                getActivity().startActivity(new Intent(getActivity(), MeAccountActivity.class));
                break;
            case R.id.rl_advice:
                txt_scan_number.setVisibility(View.GONE);
                getActivity().startActivity(new Intent(getActivity(), WhoSeeMeActivity.class));
                break;
            case R.id.rl_setting:
                getActivity().startActivity(new Intent(getActivity(), SettingActivity.class));
                break;

            case R.id.rl_setting_info:
                getActivity().startActivity(new Intent(getActivity(), MeInfoActivity.class));
                break;
            case R.id.rl_home:
                if (userModel != null) {
                    Intent intent = new Intent(getActivity(), PersonHomeActivity.class);
                    intent.putExtra("userid", userModel.id);
                    getActivity().startActivity(intent);
                }
                break;
            case R.id.txt_guanzhu:
                getActivity().startActivity(new Intent(getActivity(), FollowActivity.class));
                break;

            case R.id.rl_vip:
//                getActivity().startActivity(new Intent(getActivity(), ShareActivity.class));

                ShareUtils.onShareIntent(getContext());
//                Intent sendIntent = new Intent();
//                sendIntent.setAction(Intent.ACTION_SEND);
//                final AppConfigModel data = Hawk.get(MyConfig.SP_CACHE_CONFIG);
//
//                if (data != null) {
//                    sendIntent.putExtra(Intent.EXTRA_TEXT, data.shareStr);
//                    sendIntent.setType("text/plain");
//                    startActivity(Intent.createChooser(sendIntent, "遇见风景遇见你"));
//                }

//                shareImg("在这里离家最近","莲花老乡自己的社区","在莲花遇见你",Uri.parse("https://www.baidu.com/link?url=6sR8Qb0TFUbtefHns5QcnsH3L3hXgTuNNSQNReFkF2rPqfXnWDBPYk4EeDHn_6TEp2_mClLSKLOjOy9ebYeP-mujyCE4IKcO3xx4M2nqW6Dxgbzm0jLd8Iskj6UevzjEBjADYKJpd596DjLasnt-gVisu0FW6lJHdRQpPfgfyvSlMcVPVGOStP-oEG7vylP5rslmKFDpG4fcpDi06A3HljvS4HZPJT1uTDviH3FGqiatx_yeotWL24lq80sippcTZk9NkTsxB4QHSOhcFa6DAah4VP1uwIKHsNrDV7b11w-vESAl8bshN7PDLSAnYZswUV3WmFA2MyjQstWJHuZenwhpKFlxEgXB7ueiF5ARbEtanzorcMp6l4UjuV86wpZCe740W1FfnnitrkD2JFb9_AOW84zGbkxbPVdGFkP8skOnBkgJMWUfw1klU-djfsJGDFAguK_r_j5iYipDCUm05POOk6085rP6m5Gn5eyLKOJEakqzfokHgPs0a5Okmnb7-rrdcoVqHvGNQlefgnzlvDeE8u4NpEuJGK-Mf6PaADbLyiH_GiIMSvfr7kV4tMgvaESzLh5tPhU90rzxw7Pg1ez8RGjmVQpXju8toUt6NG5UI6UcPgdKuzTrdejfP4Iz&timg=https%3A%2F%2Fss0.bdstatic.com%2F94oJfD_bAAcT8t7mm9GUKT-xh_%2Ftimg%3Fimage%26quality%3D100%26size%3Db4000_4000%26sec%3D1545904193%26di%3D87d5c6bd57b526428360435d7b35803b%26src%3Dhttp%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2Fb219ebc4b74543a93673f0d915178a82b90114be.jpg&click_t=1545904211978&s_info=1280_630&wd=&eqid=83d2a78b0001da06000000045c24a041"));
                break;
            case R.id.txt_fans:
                Intent intent = new Intent(getActivity(), FollowActivity.class);
                intent.putExtra("followme", true);
                getActivity().startActivity(intent);
                break;
        }
    }


    private void shareImg(String dlgTitle, String subject, String content,
                          Uri uri) {
        if (uri == null) {
            return;
        }
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        if (subject != null && !"".equals(subject)) {
            intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        }
        if (content != null && !"".equals(content)) {
            intent.putExtra(Intent.EXTRA_TEXT, content);
        }

        // 设置弹出框标题
        if (dlgTitle != null && !"".equals(dlgTitle)) { // 自定义标题
            startActivity(Intent.createChooser(intent, dlgTitle));
        } else { // 系统默认标题
            startActivity(intent);
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        onRequestInfo();
        requestStatusData();
    }


    //获取阅读状态
    public void requestStatusData() {


        HttpUtils.getInstance().toSubscribe(Api.getInstance().getReadUserStatus()
                , new SimpleSubscriber<HttpResult<ReadStatus>>() {
                    @Override
                    protected void _onNext(HttpResult<ReadStatus> o) {


                        ReadStatus data = o.getData();
                        if (data != null) {

                            txt_scan_number.setVisibility(data.total > 0 ? View.VISIBLE : View.GONE);

                            if (data.total > 99) {

                                txt_scan_number.setText("+99");
                            } else {

                                txt_scan_number.setText("+" + data.total);

                            }
                        }

                    }

                    @Override
                    protected void _onError(String message) {

                    }
                });
    }


    private void onRequestInfo() {
        HttpUtils.getInstance().toSubscribe(Api.getInstance().getUseInfo()
                , new SimpleSubscriber<HttpResult<UserModel>>() {
                    @Override
                    protected void _onNext(HttpResult<UserModel> o) {


                        if (o != null && o.getData() != null) {

                            final UserModel model = o.getData();
                            userModel = model;
                            UserInfoManager.uid = model.id;
                            UserInfoManager.is_rut = model.is_rut;
                            Hawk.put(MyConfig.SP_CACHE_USERMODEL,model);
                            txtUserName.setText(model.name);
                            txtUserSign.setText(model.intro);
                            txtFans.setText("粉丝：" + model.followers_count);
                            txtGuanzhu.setText("关注：" + model.following_count);

                            Glide.with(getActivity()).load(model.avatar_url).into(ivAvator);
                        }
                    }

                    @Override
                    protected void _onError(String message) {

                    }
                });
    }


    @OnClick(R.id.rl_xiangqing)
    public void onViewClicked() {

        if (ClickUtils.isNotFastClick()) {

            getActivity().startActivity(new Intent(getActivity(), XiangQingDaTingActivity.class));
        }
    }
}
